package com.cmo.rzl.triage;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.beetl.sql.core.kit.StringKit;

import com.cmo.rzl.common.vo.LoginLog;
import com.cmo.rzl.core.base.BaseController;
import com.cmo.rzl.core.dao.Blade;
import com.cmo.rzl.core.jfinal.ext.autoroute.ControllerBind;
import com.cmo.rzl.core.jfinal.ext.shiro.ShiroKit;
import com.cmo.rzl.core.toolbox.Func;
import com.cmo.rzl.core.toolbox.kit.ShardKit;
import com.cmo.rzl.core.toolbox.log.LogManager;
import com.cmo.rzl.shine.model.Doctor;
import com.cmo.rzl.shine.model.Triage;
import com.cmo.rzl.shine.service.DoctorService;
import com.cmo.rzl.shine.service.TriageService;
import com.cmo.rzl.shine.service.impl.DoctorServiceImpl;
import com.cmo.rzl.shine.service.impl.TriageServiceImpl;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.ext.interceptor.GET;
import com.jfinal.kit.LogKit;

@Clear
@ControllerBind(controllerKey = "/client")
public class ClientController extends BaseController {
	
	TriageService triageservice = new TriageServiceImpl();
	DoctorService doctorservice=new DoctorServiceImpl();
	ShardKit shardkit = new ShardKit();
	
	@Before(GET.class)
	public void login(){
		render(clientLoginRealPath);
	}
	public void index(){
		render(clientIndexRealPath);
	}
	
	@Before(GET.class)
	public void doLogin(){
		
		String account = getPara("account");
		if(StringKit.isBlank(account)){
			renderJson(error(LOGIN_ID_NULL_MSG));
			return;
		}
		String password = getPara("password");
		if(StringKit.isBlank(password)){
			renderJson(error(PASSWORD_NULL_MSG));
			return;
		}
		String ip = shardkit.getIpAddr(getRequest());
		Triage triage = triageservice.queryTriageIp(ip);
		if (null == triage) {
			renderJson(error(TRIAGE_IP_ERR_MSG));
			return;
		}
		Doctor doctor= doctorservice.findByLogin_id(account);
		Triage tlogin = triageservice.queryLogin(ip, password);
		if (null==doctor||null == tlogin) {
			renderJson(error(LOGIN_ERROR_MSG));
			return;
		} else {
			renderJson(success(LOGIN_SUCCESS_MSG));
			String sql = "insert into tfw_login_log (LOGNAME,USERID,CLASSNAME,METHOD,CREATETIME,SUCCEED,MESSAGE) "
					+ "values ('分诊台登陆','" +doctor.getLogin_id()
					+ "','com.cmo.rzl.shine.clientController.class',"
					+ "'client/login','" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "','成功','登陆成功')";
			System.out.println(sql);
			Blade.dao().executeUpdate(sql, null);
			setSessionAttr("login_id", doctor.getLogin_id());
			setSessionAttr("tlogin_pwd", tlogin.getTriage_pwd());
		}
		return;
	}
	
	public void getUserSession(){
		if(null==getSessionAttr("login_id")||null==getSessionAttr("tlogin_pwd")){
			renderJson(error(LOGIN_ERROR_MSG));
			render(clientLoginRealPath);
			return;
		}else{
			renderJson(success(LOGIN_SUCCESS_MSG));
			render(clientIndexRealPath);
		}
	}
	public void captcha(){
		renderCaptcha();
	}
	
	public void logout() {
		doLog(ShiroKit.getSession(), "登出");
		Subject currentUser = ShiroKit.getSubject();
		currentUser.logout();
		redirect("client/login");
	}

	public void unauth() {
		if (ShiroKit.notAuthenticated()) {
			redirect("client/login");
		}
		render(noPermissionPath);
	}
	
	public void doLog(Session session, String type){
		if(!LogManager.isDoLog()){
			return;
		}
		try{
			LoginLog log = new LoginLog();
			String msg = Func.format("[sessionID]: {} [sessionHost]: {} [sessionHost]: {}", session.getId(), session.getHost(), session.getTimeout());
			log.setLogname(type);
			log.setMethod(msg);
			log.setCreatetime(new Date());
			log.setSucceed("1");
			log.setUserid(Func.format(ShiroKit.getUser().getId()));
			Blade.create(LoginLog.class).save(log);
		}catch(Exception ex){
			LogKit.logNothing(ex);
		}
	}
}
